﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using XDL.Framework.Weapons;
using XDL.Framework;
using XDL.Framework.Collections;
using JumpNFight.Simulation;
using XDL.Framework.Particles;

namespace JumpNFight.Weapons
{
    class LightSaberWeaponSystem : 
        IWeapon,
        IWeaponSystem
    {
        public const int FryEvent = 10001;

        WeaponSettings m_settings;
        World m_world;
        AudioManager m_audio;

        public WeaponSettings Settings
        {
            get 
            {
                return m_settings;
            }
        }

        public LightSaberWeaponSystem(Game game)
        {
            
        }

        public void Initialize()
        {
            m_settings = GameServices.LoadContent<WeaponSettings>("Weapons/LightSaber");
            m_audio = GameServices.GetService<AudioManager>();
            m_world = GameServices.GetService<World>();
        }

        public void Update(GameTime gameTime)
        {
            // nothing to do
        }

        public void Fire(int ownerID, Vector3 position, Vector3 direction)
        {
            BoundingBox saberBBox = new BoundingBox(
                new Vector3(position.X - 24, position.Y - 2, 0),
                new Vector3(position.X + 24, position.Y + 2, 0));
            foreach (BadGuy it in m_world.NPCs)
            {
                if (it.IsAlive && it.Moveable.Intersects(saberBBox))
                {
                    GameServices.EventDispatcher.PostEvent(it.ID, DiscGunWeaponSystem.HitEvent, new DiscHitEventArgs(ownerID, this.Settings, new Vector2(position.X,position.Y), it.Moveable.Position));
                    GameServices.EventDispatcher.PostEvent(ownerID, Player.RegisterHitEvent, null);
                }
            }
            // per-weapon sound
            m_audio.PlayCue(Settings.Name + "/Fire");
        }

        public void Clear()
        {
            // nothing to do
        }
    }
}
